YZYZ.Admin						= {};
YZYZ.Admin.Scenic 				= {};
YZYZ.Admin.Scenic.grid 			= null;
YZYZ.Admin.Scenic.store			= null;
YZYZ.Admin.Scenic.columnModel 	= null;
YZYZ.Admin.Scenic.pictureWin 	= null;
YZYZ.Admin.Scenic.editWin 		= null;
YZYZ.Admin.Scenic.formPanel		= null;
YZYZ.Admin.Scenic.groupCB 		= null;
YZYZ.Admin.Scenic.groupStore 	= null;
YZYZ.Admin.Scenic.searchName 	= null;//搜索的行程名字
YZYZ.Admin.Scenic.searchGroupCB = null;//搜索的行程分组

YZYZ.Admin.Scenic.submitSearch = function(){
	YZYZ.Admin.Scenic.grid .getStore().setBaseParam('searchName', Ext.getCmp("searchName").getValue());
	YZYZ.Admin.Scenic.grid .getStore().setBaseParam('groupId', Ext.getCmp("searchRouteGroup").getValue());
	YZYZ.Admin.Scenic.grid .getStore().load({params :{start:0,limit:15}});
};
YZYZ.Admin.Scenic.showEditWin = function(scenicId) {
	if(!YZYZ.Admin.Scenic.formPanel) {
		YZYZ.Admin.Scenic.formPanel = new Ext.form.FormPanel({
			items:[{
				xtype:'textfield',
				labelField:'名称',
				width:200,
				id:'name'
				
			},{
				xtype:'textfield',
				labelField:'地址',
				width:200,
				id:'location'
			}]
		});
	}
	if(!YZYZ.Admin.Scenic.editWin) {
		YZYZ.Admin.Scenic.editWin = new Ext.Window({
			width:600,
			height:400,
			items:[YZYZ.Admin.Scenic.formPanel]
		});
	}
};
YZYZ.Admin.Scenic.showPictureWin = function(scenicId) {
	Ext.get('ifrPicture').dom.src = Ext.get('pictureUrl').dom.value + scenicId;
	if(!YZYZ.Admin.Scenic.pictureWin) {
		YZYZ.Admin.Scenic.pictureWin =	new Ext.Window({
		 title:'查看景区图片',
		 height:500,
		 width:870,
		 autoScroll:true,
		 contentEl:'ifrPicture',
		 modal:true,
		 buttons:[{
		 	text:'关闭',
		 	handler:function(){YZYZ.Admin.Scenic.pictureWin.hide();}
		 }]
	});
	}

	YZYZ.Admin.Scenic.pictureWin.show();
};
YZYZ.Admin.Scenic.enableConfirm = function(id){
	YZYZ.Curtain.msg("系统处理中....");
	Ext.Ajax.request( {
		url		: Ext.get("enableUrl").dom.value,
		timeout	: 30000,
		method	: 'POST',
		params	: {
			scenicId:YZYZ.Admin.Scenic.grid.getSelectionModel().getSelected().get("id")
		},
		success : function(result, request) {
			result = Ext.util.JSON.decode(result.responseText);
			if(result.code == YZYZ.success){
				YZYZ.Curtain.msg("操作成功");
				YZYZ.Admin.Scenic.grid.getSelectionModel().getSelected().set("available",true);
				YZYZ.Admin.Scenic.grid.getStore().commitChanges();		
			}
			else {
				YZYZ.Curtain.alert(result.message);
			}
		},
		failure : function(result, request) {
			YZYZ.Curtain.alert("操作失败！程序错误或网络故障");
		}
	});	
};
YZYZ.Admin.Scenic.disableConfirm=function(id){
	YZYZ.Curtain.msg("系统处理中....");
	Ext.Ajax.request( {
		url		: Ext.get("disableUrl").dom.value,
		timeout	: 30000,
		method	: 'POST',
		params	: {
			scenicId:YZYZ.Admin.Scenic.grid.getSelectionModel().getSelected().get("id")
		},
		success : function(result, request) {
			result = Ext.util.JSON.decode(result.responseText);
			if(result.code == YZYZ.success){
				YZYZ.Curtain.msg("操作成功");
				YZYZ.Admin.Scenic.grid.getSelectionModel().getSelected().set("available",false);
				YZYZ.Admin.Scenic.grid.getStore().commitChanges();		
			}
			else {
				YZYZ.Curtain.alert(result.message);
			}
		},
		failure : function(result, request) {
			YZYZ.Curtain.alert("操作失败！程序错误或网络故障");
		}
	});	
};
YZYZ.Admin.Scenic.confirm=function(id){
	Ext.MessageBox.show({
		title:'确认',
		msg:'若删除，它所包含的图片以及景点也将被删除。您是否继续？',
		buttons:Ext.MessageBox.OKCANCEL,
		fn:function(btn){YZYZ.Admin.Scenic.handleDelete(btn,id);}
	});
};
YZYZ.Admin.Scenic.handleDelete = function(btn,id){
	if(btn!='ok'){
		return;
	}
	YZYZ.Curtain.msg("系统处理中....");
	Ext.Ajax.request( {
		url		: Ext.get("deleteUrl").dom.value,
		timeout	: 30000,
		method	: 'POST',
		params	: {
			scenicId:YZYZ.Admin.Scenic.grid.getSelectionModel().getSelected().get("id")
		},
		success : function(result, request) {
			result = Ext.util.JSON.decode(result.responseText);
			if(result.code == YZYZ.success){
				YZYZ.Curtain.msg("删除成功");
				YZYZ.Admin.Scenic.grid.getStore().remove(YZYZ.Admin.Scenic.grid.getSelectionModel().getSelected());	
			}
			else {
				YZYZ.Curtain.alert(result.message);
			}
		},
		failure : function(result, request) {
			YZYZ.Curtain.alert("操作失败！程序错误或网络故障");
		}
	});		
};
YZYZ.Admin.Scenic.opRender = function(value, metaData, record, rowIndex, colIndex, store){
	var html = "<div class='btnCotainer'>"
	if(value == false) {
		html = html + "<div class='divBtn' ><button type='button' onclick='YZYZ.Admin.Scenic.enableConfirm("+record.get('id')+")' class='btnDisabled divDisabled'>激活ֹ</button></div>"
	} else {
		html = html + "<div class='divBtn' ><button type='button' onclick='YZYZ.Admin.Scenic.disableConfirm("+record.get('id')+")' class='btnDisabled divEnabled'>禁止ֹ</button></div>"
	}
	html = html + "<div class='divBtn' ><button type='button' onclick='YZYZ.Admin.Scenic.showPictureWin("+record.get('id')+")' class='btnEdit'>图片</button></div>";
	html = html + "<div class='divBtn' ><button type='button' onclick='YZYZ.Admin.Scenic.showEditWin("+record.get('id')+")' class='btnEdit'>编辑</button></div>";
	html = html + "<div class='divBtn' ><button type='button' onclick='YZYZ.Admin.Scenic.showEditDescriptionWin("+record.get('id')+")' class='btnEdit'>描述</button></div>";
	//html = html + "<div class='divBtn' ><button type='button' onclick='YZYZ.Admin.Attractions.showAttPanel("+record.get('id')+")' class='btnEdit'>景点</button></div>";
	html = html + "<div class='divBtn'><button type='button' class='btnEdit' onclick='YZYZ.Admin.Scenic.confirm("+record.get('id')+")'>删除</button></div>";
	html = html + "</div>"
	return html;
};
YZYZ.Admin.Scenic.rednerGroup = function(value, metaData, record, rowIndex, colIndex, store){
	var count = YZYZ.Admin.Scenic.groupStore.getCount();
	for(var i = 0;i < count; i ++ ){
		var rec = YZYZ.Admin.Scenic.groupStore.getAt(i);
		if(parseInt(rec.get("id")) == parseInt(value)){
			return rec.get("name");
		}
	}
};
YZYZ.Admin.Scenic.init = function(){
	YZYZ.Admin.Scenic.searchName = new Ext.form.TextField({
		width:300,
		id:'searchName',
		fieldLabel:'行程名',
		renderTo:'divSearchName'
	});
	YZYZ.Admin.Scenic.groupStore = new Ext.data.Store({  
			proxy: new Ext.data.HttpProxy ({
	            url: document.getElementById("getRegionsForScenicUrl").value
	        }),
	        reader: new Ext.data.JsonReader({
	        	root : "regions"
        	}, [
                {name:'id'},
                {name:'name'}
            ]),
	        remoteSort: false
		});
	YZYZ.Admin.Scenic.groupStore.load();
	
	YZYZ.Admin.Scenic.groupCB = new Ext.form.ComboBox({  
	         id: 'addRegion', 
	         name:'addRegion', 
	         allowBlank: false,  
	         fieldLabel: "地区",  
	         hiddenName: 'hidAddRegion',  
	         displayField: 'name',  
	         valueField: 'id',  
	         typeAhead: true,  
	         triggerAction: 'all',  
	         store: YZYZ.Admin.Scenic.groupStore,  
	         mode: 'local',
	         width:300 ,
	         editable:false
 		});
 	 YZYZ.Admin.Scenic.searchGroupCB = new Ext.form.ComboBox({  
	         id: 'searchRouteGroup', 
	         name:'searchRouteGroup', 
	         allowBlank: false,  
	         fieldLabel: "行程板块",  
	         hiddenName: 'groupId',  
	         displayField: 'name',  
	         valueField: 'id',  
	         typeAhead: true,  
	         triggerAction: 'all',  
	         store: YZYZ.Admin.Scenic.groupStore,  
	         mode: 'local',
	         width:300 ,
	         editable:false,
	         renderTo:'divSearchGroup'
 		});
	YZYZ.Admin.Scenic.columnModel = new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer(),
            {header: '名称', width: 200, sortable: false, hideable:false, dataIndex: 'name'},
            {header: '地址', width: 150, sortable: false, hideable:true, dataIndex: 'location'},
            {header: '显示级别', width: 50, sortable: false, hideable:false, dataIndex: 'level'},
            {header: '组名', width: 100, sortable: false, hideable:false, dataIndex: 'ownerId', renderer:YZYZ.Admin.Scenic.rednerGroup},
            {header: '操作',id:'operations', width: 300, sortable: false, hideable:false, dataIndex: 'available', renderer:YZYZ.Admin.Scenic.opRender}
    	]);
	YZYZ.Admin.Scenic.store = new Ext.data.Store({  
			proxy: new Ext.data.HttpProxy ({
	            url: document.getElementById("getScenicsUrl").value
	        }),
	        reader: new Ext.data.JsonReader({
	        	root			: "data",
	        	totalProperty	: "sum"
        	}, [
                {name:'id'},
                {name:'name'},
                {name:'location'},
                {name:'level', type:'int'},
                {name:'available',type:'boolean'},
                {name:'ownerId',type:'string'}
            ]),
	        remoteSort: false
		});
	YZYZ.Admin.Scenic.grid = new Ext.grid.GridPanel({
			id			: "scenicGrid",
			store		: YZYZ.Admin.Scenic.store,
			cm			: YZYZ.Admin.Scenic.columnModel,
			sm			: new Ext.grid.RowSelectionModel(),
			region		: 'center',
			height		: Ext.getBody().getHeight()-100,
			width		: Ext.getBody().getWidth(),
			viewConfig	: { forceFit:true },
			autoExpandColumn : 'operations',
			loadMask	: { msg:"数据加载中....." },
			bbar		: new Ext.PagingToolbar({   
				pageSize	: 15,   
				store		: YZYZ.Admin.Scenic.store,   
				displayInfo	: true,   
				displayMsg	: "第{0}条到{1}条  共{2}条",   
				emptyMsg	: "没有景区",
				items		:[{text:'添加景区',handler:YZYZ.Admin.Scenic.showNewWin}]
			})              
		});
	new Ext.Viewport({
		layout: 'border',
		items:[{
			region			: 'center',
			cls				: 'divGrid',
			items 			: [YZYZ.Admin.Scenic.grid],
			autoScroll		: false,
			layout			: 'fit' 
		},{
			region			: 'north',
			contentEl 		: 'divNorth',
			autoScroll		: false,
			layout			: 'fit',
			height 			: 100
		},]
	});
	YZYZ.Admin.Scenic.grid.getStore().load({params :{start:0,limit:15}});
};
Ext.onReady(YZYZ.Admin.Scenic.init, document);